package com.coco.seck.mqqueue;

import com.coco.common.constant.Constant;
import com.coco.common.util.IdWorker;
import com.coco.seck.mapper.SeckillGoodsMapper;
import com.coco.seck.mapper.SeckillOrderMapper;
import com.coco.seck.pojo.TbSeckillGoods;
import com.coco.seck.pojo.TbSeckillOrder;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.redisson.Redisson;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author HanSama
 * @date 2021年07月18日 17:07
 */
@Component
@Slf4j
public class SecOrderQueue {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";

    public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";

    public static final int DELAY_TIME = 1000*60*10;

    @RabbitListener(queues = "secOrderOne")
    @RabbitHandler
    public void secOrderOne(HashMap<String,String> map){
       try{
           addOrder(map);
       }catch (Exception e){

       }
    }

//    @RabbitListener(queues = "secOrderTwo")
//    @RabbitHandler
//    public void secOrderTwo(HashMap<String,String> map){
//        try{
//            addOrder(map);
//        }catch (Exception e){
//
//        }
//    }
//
//    @RabbitListener(queues = "secOrderThree")
//    @RabbitHandler
//    public void secOrderThree(HashMap<String,String> map){
//        try{
//            addOrder(map);
//        }catch (Exception e){
//
//        }
//    }


    private void addOrder(Map<String,String> map){

        //获取对应的ID
        String userId = map.get(Constant.OrderKeyConstant.USERKEY);
        String seckillId = map.get(Constant.OrderKeyConstant.GOODSKEY);
        String uuid = map.get(Constant.OrderKeyConstant.ORDERKEY);

        //通过秒杀商品id获取秒杀商品
        TbSeckillGoods seckillGoods = seckillGoodsMapper.findOneById(Long.parseLong(seckillId));

        //属性赋值
        TbSeckillOrder seckillOrder = new TbSeckillOrder();
        seckillOrder.setSeckillId(Long.parseLong(seckillId));
        seckillOrder.setId(Long.parseLong(uuid));
        seckillOrder.setMoney(seckillGoods.getCostPrice());
        seckillOrder.setUserId(Long.parseLong(userId));
        seckillOrder.setCreateTime(new Date());

        //初始化订单
        seckillOrderMapper.addOrder(seckillOrder);

        //记录order未支付过期状态 (Redis)
//        redisTemplate.opsForValue().set("orderKey:"+uuid,"",10, TimeUnit.SECONDS);

        //RabbitMQ 延迟队列 设置订单过期
        rabbitTemplate.convertAndSend(DELAYED_EXCHANGE_NAME,DELAYED_ROUTING_KEY,
                uuid,
                correlationData ->{
            //设置10 分钟过期
                    correlationData.getMessageProperties().setDelay(DELAY_TIME);
                    return correlationData;
                });
        log.info(" 当 前 时 间 ： {}, 发送一条延迟 {} 毫秒的信息给队列 delayed.queue:{}", new
                Date(),DELAY_TIME, uuid);
    }
}
